#!/bin/sh
#(c) Barry Kauler 2005/2006 www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#keyboard/mouse wizard for Puppy Linux
#based on code from /usr/X11R7/bin/xwin
#updated for 2.17
#v411 rerwin: touchpad fix.
#v411 rerwin: mouse detect fixes.
#2009-03-03 ecube: "requires Xorg driver" added for Left-hand mouse
#2009-05-23 ecube: kmap-1.1.14 keymaps used
#v431 BK: usb and ps/2 mouse now has same devnode.
#091122 BK: not using 'kbd' pkg keycodes anymore. only kmaps in /lib/keymaps.
#100127 notice to restart X if change keyboard layout (see code in /usr/bin/xwin to sync x and console).
#100215 moved sync code for console/xorg kbd layout from xwin.
#100527 added jemimah's flsynclient touchpad config.
#101112 disabled FONTMAP, see http://bkhome.org/blog/?viewDetailed=01956
#110814 01micko: convert script from gtkdialog2 to gtkdialog3+.
#120201 rodin.s: internationalized.
#120627 use keymaps from kbd pkg if present.
#130216 add button for "mouse sensitivity", run pupx.
#131212 zigbert: gui (gtkdialog) improvements.
#131230 zigbert: use quicksetup (via chooselayout) instead of internal keyboard layout setter

export TEXTDOMAINDIR=/usr/share/locale
export TEXTDOMAIN=input-wizard
export OUTPUT_CHARSET=UTF-8
. gettext.sh

export MouseKeyboardWizard='
<window title="'$(gettext 'Input Wizard')'" icon-name="gtk-preferences">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info fixed mouse_keyboard.svg 60 " " "$(gettext "Configure your input devices")"`"'
  <hbox space-expand="true" space-fill="true">
    <frame '$(gettext 'Keyboard')'>
      <hbox>
        <button space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon country_keyboard.svg big`"'
          <action type="exit">KEY1BUTTON</action>
        </button>
        <text space-expand="false" space-fill="false"><label>'$(gettext 'Layout for your country')'</label></text>
        <text space-expand="true" space-fill="true"><label>""</label></text>
      </hbox>
      <hbox>
        <button space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon keyboard.svg big`"'
          <action type="exit">KEY3BUTTON</action>
        </button>
        <text space-expand="false" space-fill="false"><label>'$(gettext 'Keyboard interface')'</label></text>
        <text space-expand="true" space-fill="true"><label>""</label></text>
      </hbox>
      <hbox>
        <button space-expand="false" space-fill="false">
          '"`/usr/lib/gtkdialog/xml_button-icon keyboard_shortcut.svg big`"'
          <action type="exit">KEY4BUTTON</action>
        </button>
        <text space-expand="false" space-fill="false"><label>'$(gettext 'Advanced configuration')'</label></text>
        <text space-expand="true" space-fill="true"><label>""</label></text>
      </hbox>
    </frame>

    <frame '$(gettext 'Pointer')'>
      <hbox>
        <text><label>'$(gettext 'Type of mouse')'</label></text>
        <button>
          '"`/usr/lib/gtkdialog/xml_button-icon mouse.svg big`"'
          <action type="exit">MOUSE1BUTTON</action>
        </button>
      </hbox>
      <hbox>
        <text><label>'$(gettext 'Adjust mouse sensitivity')'</label></text>
        <button>
          '"`/usr/lib/gtkdialog/xml_button-icon mouse_sensitivity.svg big`"'
          <action type="exit">MOUSE2BUTTON</action>
        </button>
      </hbox>
      <hbox>
        <text><label>'$(gettext 'Touchpad')'</label></text>
        <button>
          '"`/usr/lib/gtkdialog/xml_button-icon touchpad.svg big`"'
          <action type="exit">TOUCHPAD1BUTTON</action>
        </button>
      </hbox>
    </frame>
  </hbox>

  <hbox space-expand="false" space-fill="false">
    <button>
      '"`/usr/lib/gtkdialog/xml_button-icon quit`"'
      <label>'$(gettext 'Quit')'</label>
    </button>
  </hbox>
 </vbox>
</window>'

CHECK1="false"

#big while loop...
while :; do
 unset MouseWizard
 unset KeyboardFeaturesWizard

 #put some code in here to ask keyboard layout, keyboard ps2/usb, mouse serial/ps2/usb setup?
 #also offer the old 2-letter layout...
  . /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
  eval `gtkdialog -p MouseKeyboardWizard | grep "EXIT"`

 case $EXIT in
  KEY1BUTTON)
   chooselayout
  ;;
  KEY4BUTTON)
   exec /usr/local/bin/xkbconfigmanager
  ;;
  KEY3BUTTON)
   KEYBRDTYPE="`cat /etc/keyboardtype`"
   CHECKKEY1="false"
   [ "$KEYBRDTYPE" = "usb" ] && CHECKKEY1="true"
   export KeyboardFeaturesWizard='
<window title="'$(gettext 'Input Wizard')'" icon-name="gtk-preferences">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info scale keyboard.svg 60 "$(gettext "Set interface for your keyboard")" "$(gettext "Changing this only takes effect after Puppy is rebooted!")"`"'
  <vbox space-expand="false" space-fill="false">
    <frame '$(gettext 'Interface')'>
      <text height-request="20"><label>""</label></text>
      <checkbox>
        <label>'$(gettext 'Keyboard has USB interface')'</label>
        <default>'$CHECKKEY1'</default>
        <variable>CHECKBOX002</variable>
      </checkbox>
      <text height-request="5"><label>""</label></text>
    </frame>
  </vbox>
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Cancel")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <action>exit:CANCEL</action>
    </button>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Ok")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <action>exit:OK</action>
    </button>
  </hbox>
</vbox>
</window>'

   . /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
   RETPARAMS=`gtkdialog -p KeyboardFeaturesWizard`
   echo "$RETPARAMS"
   if [ ! "`echo "$RETPARAMS" | grep "EXIT" | grep "OK"`" = "" ];then
    if [ "`echo "$RETPARAMS" | grep "CHECKBOX002" | grep "true"`" != "" ];then
     echo -n "usb" > /etc/keyboardtype
     modprobe usbhid 2> /dev/null #maybe then just need to restart X.
    else
     echo -n "ps/2" > /etc/keyboardtype
    fi
   fi
  ;;
  TOUCHPAD1BUTTON) #100527
   exec flsynclient
  ;;
  MOUSE2BUTTON) #130216
   exec pupx
  ;;
  MOUSE1BUTTON)
   MOUSEDEV="`cat /etc/mousedevice`"
   #RADIOBUTTONS="$RADIO2$RADIO1$RADIO3" #serial
   DEF1="false"
   DEF2="true"
   DEF3="false"
   if [ "$MOUSEDEV" = "psaux" ];then
    #RADIOBUTTONS="$RADIO1$RADIO2$RADIO3"
    DEF1="true"
    DEF2="false"
    DEF3="false"
   fi
   if [ "$MOUSEDEV" = "input/mice" ];then
    #RADIOBUTTONS="$RADIO3$RADIO2$RADIO1"
    DEF1="false"
    DEF2="false"
    DEF3="true"
   fi

   MOUSEBUTTONS="`cat /etc/mousebuttons`"
   CHECK1="false"
   if [ "$MOUSEBUTTONS" = "5" ];then
    CHECK1="true"
   fi

   #v1.0.7
   CHECKM="false"
   if [ ! "`cat /etc/xextraoptions | grep "\\-2button"`" = "" ];then
    CHECKM="true"
   fi
   #v2.01
   CHECKH="false"
   IDLETIME="0"
   if [ -f /etc/mousehide ];then
    IDLETIME="`cat /etc/mousehide | cut -f 1 -d ','`"
    [ ! "$IDLETIME" = "0" ] && CHECKH="true"
   fi

   #v2.14 modified by Raul Suarez (rarsa at yahoo.com)
   # if the button 1 is mapped to something different than 1 then I assume
   # it is a lefhanded mouse
   CHECKL="false"
   LEFT_H_MOUSE=` xmodmap -pp | grep -E " *[0-9] *[0-9]" | sed -e 's/ *\([0-9]*\) *\([0-9]*\)/\2/' | head -n 1`
   if [ ${LEFT_H_MOUSE} -ne 1 ] ; then
    CHECKL="true"
   fi

   #v4.11 Control "tapping" function on ALPS/Synaptics touchpads - manipulates MaxTapTime option in xorg.conf.
   TAPCHKBOX=""
   if [ -f /etc/X11/xorg.conf -a "`grep '"MaxTapTime"' /etc/X11/xorg.conf`" != "" ];then #touchpad present
    [ "`grep '"MaxTapTime"' /etc/X11/xorg.conf | grep '"0"'`" = "" ] && CHECKT="true" || CHECKT="false" #taps enabled/disabled
    TAPCHKBOX="    <checkbox>
     <label>$(gettext 'Enable touchpad tapping for mouse button clicks')</label>
     <variable>CHECKBOX013</variable>
     <default>$CHECKT</default>
    </checkbox>
"
   fi  #v4.11 end
  
   export MouseWizard='
<window title="'$(gettext 'Mouse Wizard')'" icon-name="gtk-preferences">
<vbox space-expand="true" space-fill="true">
  '"`/usr/lib/gtkdialog/xml_info fixed mouse.svg 60 "<b>$(gettext "Configure your mouse.")</b>" "$(gettext "If you got a touchpad, choose USB interface.")"`"'
  <hbox space-expand="true" space-fill="true">
  <hbox space-expand="false" space-fill="false">
  <vbox  space-expand="true" space-fill="true">
    <frame '$(gettext 'Interface')'>
      <hbox space-expand="false" space-fill="false">
        <vbox space-expand="false" space-fill="false">
           <text height-request="1"><label>""</label></text>
           <radiobutton height-request="45">
            <label>'$(gettext 'USB')'</label>
            <variable>RADIO003</variable>
            <default>'$DEF3'</default>
            <action>if true disable:COMPORT</action>
          </radiobutton>
          <radiobutton height-request="45">
            <label>'$(gettext 'PS/2')'</label>
            <variable>RADIO001</variable>
            <default>'$DEF1'</default>
            <action>if true disable:COMPORT</action>
          </radiobutton>
          <radiobutton height-request="45">
            <label>'$(gettext 'Serial')'</label>
            <variable>RADIO002</variable>
            <default>'$DEF2'</default>
            <action>if true enable:COMPORT</action>
          </radiobutton>
        </vbox>
        <text space-expand="true" space-fill="true"><label>""</label></text>
        <vbox spacing="0" space-expand="false" space-fill="false">
         <pixmap><input file>/usr/share/pixmaps/puppy/plug_usb.svg</input><height>50</height></pixmap>
         <pixmap><input file>/usr/share/pixmaps/puppy/plug_ps2_mouse.svg</input><height>50</height></pixmap>
         <pixmap><input file>/usr/share/pixmaps/puppy/plug_serial.svg</input><height>50</height></pixmap>
        </vbox>
        <text width-request="5"><label>""</label></text>
      </hbox>
        <text height-request="5"><label>""</label></text>
          <hbox space-expand="false" space-fill="false">
            <text><label>'$(gettext 'Port')'</label></text>
            <combobox width-request="90">
              <item>ttyS0</item>
              <item>ttyS1</item>
              <item>ttyS2</item>
              <item>ttyS3</item>
              <variable>COMPORT</variable>
              <visible>disabled</visible>
            </combobox>
          </hbox>
    </frame>
  </vbox>
  </hbox>
  <vbox space-expand="true" space-fill="true">
    <frame '$(gettext 'Features')'>
      <text height-request="5"><label>""</label></text>
      <checkbox>
        <label>'$(gettext 'Mouse has a scroll wheel')'</label>
        <variable>CHECKBOX009</variable>
        <default>'$CHECK1'</default>
      </checkbox>
      <checkbox>
        <label>'$(gettext 'Middle button emulated on a 2-button mouse')'</label>
        <variable>CHECKBOX010</variable>
        <default>'$CHECKM'</default>
      </checkbox>
      <checkbox>
        <label>'$(gettext 'Auto-hide mouse cursor when not moving')'</label>
        <variable>CHECKBOX011</variable>
        <default>'$CHECKH'</default>
      </checkbox>
      <checkbox>
        <label>'$(gettext 'Left-handed mouse (requires Xorg driver)')'</label>
        <variable>CHECKBOX012</variable>
        <default>'$CHECKL'</default>
      </checkbox>
      '$TAPCHKBOX'
      <text height-request="5"><label>""</label></text>
    </frame>
  </vbox>
  </hbox>
  <hbox space-expand="false" space-fill="false">
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Cancel")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon cancel`"'
      <action>exit:CANCEL</action>
    </button>
    <button space-expand="false" space-fill="false">
      <label>'$(gettext "Ok")'</label>
      '"`/usr/lib/gtkdialog/xml_button-icon ok`"'
      <action>exit:OK</action>
    </button>
  </hbox>
</vbox>
</window>'

   . /usr/lib/gtkdialog/xml_info gtk #build bg_pixmap for gtk-theme
   RETPARAMS=`gtkdialog -p MouseWizard`
   echo "$RETPARAMS"
   if [ ! "`echo "$RETPARAMS" | grep "EXIT" | grep "OK"`" = "" ];then
    if [ -e /lib/modules/2.6 ];then #v1.0.2 k2.6...
     modprobe -r psmouse 2> /dev/null #v1.0.2 k2.6
     modprobe -r sermouse 2> /dev/null #v1.0.2 k2.6
    else
     #modprobe -r usbmouse 2> /dev/null #v1.0.2
     modprobe -r mousedev #v2.0.0
    fi
#    if [ ! "`echo "$RETPARAMS" | grep "RADIO001" | grep "true"`" = "" ];then
#     MOUSEDEV="psaux" #ps/2 mouse.
#     ln -snf psaux /dev/mouse #v411
#     #k2.4 has ps/2 mouse support inbuilt.
#     if [ -e /lib/modules/2.6 ];then #v1.0.2 k2.6...
#      modprobe psmouse 2> /dev/null #v1.0.2 k2.6
#     fi
#    fi
    if [ ! "`echo "$RETPARAMS" | grep "RADIO002" | grep "true"`" = "" ];then
     eval `echo "$RETPARAMS" | grep "COMPORT"`
     MOUSEDEV="$COMPORT" #ttyS0,1,2,3
     ln -snf $COMPORT /dev/mouse #v411
     #2.4 kernel has serial mouse support inbuilt.
     if [ -e /lib/modules/2.6 ];then #v1.0.2 k2.6...
      modprobe 8250_pci 
      modprobe sermouse 
     fi
    fi
    if [ ! "`echo "$RETPARAMS" | grep -E "RADIO001|RADIO003" | grep "true"`" = "" ];then #use this for both usb and ps2 mouse
     MOUSEDEV="input/mice" #usb mouse
     if [ -e /lib/modules/2.6 ];then #v1.0.2 k2.6...
      modprobe usbhid 2> /dev/null
     else
      #modprobe usbmouse
      modprobe hid #v2.0.0
      modprobe mousedev #v2.0.0
     fi
     ln -snf input/mice /dev/mouse #v411
    fi
    echo -n "$MOUSEDEV" > /etc/mousedevice

    #v4.11 Touchpad tapping control - sets xorg.conf MaxTapTime to 0 to disable tapping and to default or user-specified xorg.conf value to enable.
    if [ "`echo "$RETPARAMS" | grep "CHECKBOX013"`" != "" ];then
     ZEROTAPTIME="`grep '"MaxTapTime"' /etc/X11/xorg.conf | grep '"0"'`"
     if [ "`echo "$RETPARAMS" | grep "CHECKBOX013" | grep "true"`" != "" ];then #enable tapping
      if [ "$ZEROTAPTIME" != "" ];then #currently disabled, else leave as is
       [ -f /root/.maxtaptime ] && MAXTAPTIME="`grep -m 1 '"MaxTapTime"' /root/.maxtaptime`" || MAXTAPTIME='	Option "MaxTapTime" "220"'
       SEDSCRIPT="s/$ZEROTAPTIME/$MAXTAPTIME/"
       sed -i -e  "$SEDSCRIPT" /etc/X11/xorg.conf
      fi
     else #disable tapping
      if [ "$ZEROTAPTIME" = "" ];then #currently enabled, else leave as is
       MAXTAPTIME="`grep '"MaxTapTime"' /etc/X11/xorg.conf`"
       if [ "$MAXTAPTIME" != "" ];then
        echo "$MAXTAPTIME" > /root/.maxtaptime
        ZEROTAPTIME='	Option "MaxTapTime" "0"'
        SEDSCRIPT="s/$MAXTAPTIME/$ZEROTAPTIME/"
        sed -i -e  "$SEDSCRIPT" /etc/X11/xorg.conf
       fi
      fi
     fi
    fi     
    #v4.11 end

    #v2.14 modified by Raul Suarez (rarsa at yahoo.com)
    # sets right or lefhanded mouse and saves it for next boot.
    if [ ! "`echo "$RETPARAMS" | grep "CHECKBOX012" | grep "true"`" = "" ];then #v2.01
     XMODMAP_CMD="pointer = 3 2 1 4 5"
    else
     XMODMAP_CMD="pointer = 1 2 3 4 5"
    fi
    xmodmap -e "${XMODMAP_CMD}"
    grep -v "^pointer" /root/.Xmodmap > /tmp/Xmodmap.tmp
    cp /tmp/Xmodmap.tmp /root/.Xmodmap
    echo "${XMODMAP_CMD}" >> /root/.Xmodmap
    #

    if [ ! "`echo "$RETPARAMS" | grep "CHECKBOX011" | grep "true"`" = "" ];then #v2.01
     [ "$IDLETIME" = "0" ] && IDLETIME="3"
     echo -n "${IDLETIME}," > /etc/mousehide
     [ "`pidof unclutter`" = "" ] && unclutter -idle $IDLETIME &
    else
     echo -n '0,' > /etc/mousehide
     [ ! "`pidof unclutter`" = "" ] && killall unclutter
    fi

    #v1.0.7 emulate middle button
    #v2.02 Dougal has suggested changes for 3-button emulation...
    MBUTOUT="`cat /etc/xextraoptions | sed -e 's/ \-[23]button//g'`"
    if [ ! "`echo "$RETPARAMS" | grep "CHECKBOX010" | grep "true"`" = "" ];then
     #'-2button' tells Xvesa that want 3rd-button emulation...
     echo -n "$MBUTOUT -2button" > /etc/xextraoptions
     echo -n "2" > /etc/mousebuttons
     if [ -f /etc/X11/xorg.conf ];then
      #\W\+ means one or more whitespace chars...
      cat /etc/X11/xorg.conf | tr -s '#' | sed -e 's/#Option\W\+"Emulate3Buttons"/Option      "Emulate3Buttons"/g' > /tmp/xorg.conf.tmp2
      sync
      cat /tmp/xorg.conf.tmp2 | tr -s '#' | sed -e 's/#Option\W\+"Emulate3Timeout"/Option      "Emulate3Timeout"/g' > /tmp/xorg.conf.tmp
     fi
    else
     echo -n "$MBUTOUT" > /etc/xextraoptions
     echo -n "3" > /etc/mousebuttons
     if [ -f /etc/X11/xorg.conf ];then
      cat /etc/X11/xorg.conf | sed -e 's/Option\W\+"Emulate3Buttons"/#Option      "Emulate3Buttons"/g' | tr -s '#' > /tmp/xorg.conf.tmp2
      sync
      cat /tmp/xorg.conf.tmp2 | sed -e 's/Option\W\+"Emulate3Timeout"/#Option      "Emulate3Timeout"/g' | tr -s '#' > /tmp/xorg.conf.tmp
     fi
    fi
    sync
    if [ -f /tmp/xorg.conf.tmp ];then
     cp -f /tmp/xorg.conf.tmp /etc/X11/xorg.conf
    fi

    if [ ! "`echo "$RETPARAMS" | grep "CHECKBOX009" | grep "true"`" = "" ];then
     echo -n "5" > /etc/mousebuttons #enable scrollwheel.
     if [ -f /etc/X11/xorg.conf ];then
      cat /etc/X11/xorg.conf | tr -s '#' | sed -e 's/#Option\W\+"ZAxisMapping"\W\+"4 5"/Option      "ZAxisMapping" "4 5"/g' > /tmp/xorg.conf.tmp
     fi
    else
     # echo -n "3" > /etc/mousebuttons ...v1.0.7 moved up.
     if [ -f /etc/X11/xorg.conf ];then
      cat /etc/X11/xorg.conf | sed -e 's/Option\W\+"ZAxisMapping"\W\+"4 5"/#Option      "ZAxisMapping" "4 5"/g' | tr -s '#' > /tmp/xorg.conf.tmp
     fi
    fi
    if [ -f /etc/X11/xorg.conf ];then
     cp -f /tmp/xorg.conf.tmp /etc/X11/xorg.conf
    fi
    sync

    #v411 Moved xorg.conf protocol update to xwin, where it also supports changes by rc.sysinit.

   fi
  ;;

  *)
   break
  ;;
 esac

done #end while loop.
unset MouseKeyboardWizard
unset MouseWizard
unset KeyboardFeaturesWizard

###END###
